<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共166条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger" @click="outemployees">导出</el-button>
          <el-button size="small" type="primary" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" type="index" sortable="">
            <template #default="scope">
              {{ (pages.page-1) * pages.size + scope.$index+1 }}
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column
            width="120"
            label="聘用形式"
            prop="formOfEmployment"
            :formatter="formOfEmployment"
            sortable=""
          />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column width="120" label="入职时间" sortable="">
            <template #default="scope">
              {{ scope.row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column width="120" label="账户状态" sortable="">
            <template #default="scope">

              <el-switch
                :value="scope.row.enableState===1"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="scope">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${scope.row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delemployee(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="pages.total"
            :page-size="pages.size"
            :current-page="pages.page"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <AddEmployee :show-dialog="showDialog" />
    </div>
  </div>
</template>

<script>
import { getemployeesList, delemployees } from '@/api/employees'
import AddEmployee from '@/views/employees/components/add-employee.vue'
import employees from '@/api/contant/employees'
import { formatDate } from '@/filters/index'
export default {
  components: {
    AddEmployee
  },
  data() {
    return {
      loading: false,
      list: [],
      pages: {
        page: 1,
        size: 5,
        total: 0
      },
      showDialog: false
    }
  },
  created() {
    this.getemployeesList()
  },
  methods: {
    async getemployeesList() {
      const { total, rows } = await getemployeesList(this.pages)
      this.pages.total = total
      this.list = rows.map(item => {
        return {
          ...item,
          enableState: item.enableState || 1
        }
      })

      // console.log(this.list)
    },
    currentChange(newPage) {
      this.pages.page = newPage
      this.getemployeesList()
    },
    formOfEmployment(row, col, cell, index) {
      const obj = employees.hireType.find(item => item.id === cell)
      return obj ? obj.value : '未知形式'
    },
    async outemployees() {
      // console.log(123)

      const { export_json_to_excel } = await import('@/vendor/Export2Excel')

      const dict = {
        '姓名': 'username',
        '入职日期': 'timeOfEntry',
        '工号': 'workNumber',
        '手机号': 'mobile',
        '转正日期': 'correctionTime',
        '部门': 'departmentName',
        '是否正式': 'formOfEmployment'
      }
      const header = Object.keys(dict)
      // console.log(dict)
      const { rows } = await getemployeesList({ page: this.pages.page, size: this.pages.total })
      // console.log(rows)
      const newObj = []
      rows.map(item => {
        const res = this.ObjectBcd(item, dict)
        newObj.push(res)
      })
      console.log(header)
      console.log(newObj)
      export_json_to_excel({
        header,
        data: newObj
      })
    },
    // 转换格式
    ObjectBcd(item, dict, header) {
      // console.log(item)
      const arr = []
      for (const key in dict) {
        const enkey = dict[key]

        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          // console.log(3333)
          // console.log(dict[enkey])
          const en = formatDate(item[enkey], 'yyyy-MM-dd')
          arr.push(en)
        } else if (enkey === 'formOfEmployment') {
          const obj = employees.hireType.find(ite => ite.id === item[enkey])

          const res = obj ? obj.value : '未知形式'
          arr.push(res)
          // console.log(item[enkey])
        } else {
          const ts = item[enkey] ? item[enkey] : '未知形式'
          arr.push(ts)
        }
      }
      return arr
    },
    async delemployee(id) {
      // await delemployees(id)
      try {
        await this.$confirm('请确认是否删除')
        // console.log(id)
        await delemployees(id)
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.pages.page > 1) {
          this.pages.page--
        }
        this.getemployeesList()
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style>

</style>
